import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

class StreamBuiderPage extends StatefulWidget {
  const StreamBuiderPage({Key? key}) : super(key: key);

  @override
  State<StreamBuiderPage> createState() => _StreamBuiderPageState();
}

class _StreamBuiderPageState extends State<StreamBuiderPage> {
  int _count = 0;
  // 流stream 控制器
  StreamController<int> streamController = StreamController();

  @override
  void dispose() {
    // 销毁
    streamController.close();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: (){
          setState(() {
            _count++;
            // 发送消息
            streamController.add(_count);
          });
        },
      ),
      appBar: AppBar(
        title: Text("StreamBuilder 实现页面的局部刷新"),
      ),
      body: Container(
        padding: EdgeInsets.all(30),
        child: Column(
          children: [
            // 接收消息
            StreamBuilder<int>(
              // 初始值
              initialData: _count,
              // 绑定stream
              stream: streamController.stream,
              builder: (BuildContext context,AsyncSnapshot<int> snapshot){
                return Text("测试使用 ${snapshot.data}");
            }),
            
            Text("测试使用"),
            Text("测试使用"),
          ],
        ),
      ),
    );
    
  }
}